package com.katze.boot.plugins.security.config;

import com.katze.boot.plugins.security.filter.IPathMatcher;
import com.katze.common.env.AppEnvironment;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(prefix = "springdoc", name = {"swagger-ui.enabled", "api-docs.enabled"}, havingValue = "true", matchIfMissing = true)
public class SpringDocConfig {

    @Bean
    public OpenAPI customOpenApi() {
        SecurityRequirement security = new SecurityRequirement();
        security.addList("security");

        Components components = new Components()
                // 设置 spring security jwt accessToken 认证的请求头 Authentication
                .addSecuritySchemes("security", new SecurityScheme().in(SecurityScheme.In.HEADER).name(IPathMatcher.TOKEN_NAME).type(SecurityScheme.Type.APIKEY));
//                .bearerFormat("JWT").in(SecurityScheme.In.HEADER).name("Authentication").scheme("Bearer"));

        License license = new License().name("Apache 2.0").url("https://www.apache.org/licenses/LICENSE-2.0.html");

        Info info = new Info()
                .title("Katze后台管理系统")
                .version(AppEnvironment.APP_VERSION.getValue())
                .license(license);
        return new OpenAPI()
                .info(info)// 设置标题、版本等信息
                .components(components)
                .addSecurityItem(security);
    }
}
